<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\UserInfo;
use App\Shop;
use App\ShopUser;
use DB;
use Input;
use Mockery\Exception;

class UserInfoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $datas  = UserInfo::select('uid','username','mobile','nickname','regdate','status','sex')
                ->paginate('30');
        return view('admin.userinfo.index',compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $datas = UserInfo::select('uid','username','mobile','nickname','regdate','sex','avatar','birthday','regcity','status')
                 ->where('uid',$id)
                 ->first();
        $datas['shop'] = DB::table('shop_users')
                     ->where('shop_users.userid',$datas['uid'])
                     ->select('shops.shopname')
                     ->join('shops','shop_users.shopid','=','shops.id')
                     ->get();
        //获取地区进行转化;因为$datas['regcity']转码了;
        $datas['regcity'] = json_decode($datas['regcity']);
        return view('admin.userinfo.show',compact('datas'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = UserInfo::where('uid',$id)->delete();
        //删除shop_users表中的对应关系
//       try{
//           ShopUser::where('userid',$id)->delete();
//       }catch(Exception $e){
//           print $e->getMessage();
//           exit();
//       }

        if($res)
        return Response() -> json(['status'=>'success','msg'=>'删除成功']);
        return Response() -> json(['status'=>'false','msg'=>'删除成功']);
    }
    public function change_status()
    {
        /**
         * @uid  userinfo 中的uid
         * @param
         */
        $uid = Input::get('uid');
        //根据uid获取当前的status值
        $status = UserInfo::where('uid',$uid)->value('status');
        if($status == 1){
            //为1的时候表示现在不是禁用状态
            $res = UserInfo::where('uid',$uid)->update(['status'=>'0']);
            if($res)
            return Response() -> json(['status'=>'success_0','msg'=>"更新成功"]);
            return Response() -> json(['status'=>'false','msg'=>"更新失败"]);
        }else{
            //不为1时候表示是禁用状态
            $res = UserInfo::where('uid',$uid)->update(['status'=>'1']);
            if($res)
            return Response() -> json(['status'=>'success_1','msg'=>"更新成功"]);
            return Response() -> json(['status'=>'false','msg'=>"更新失败"]);

        }

    }
    public function fromshop($uid)
    {
        //根据uid查询shop_users
        $datas  = DB::table('shop_users')
            ->join('shops','shop_users.shopid','=','shops.id')
            ->where('shop_users.userid',$uid)
            ->select('shops.shopname','shops.id')
            ->get();
        return view('admin.userinfo.fromshop',compact('datas'));

    }

}
